package com.songtech.server.frame.service;

import com.songtech.server.Jedis.JedisBaseDao;
import com.songtech.server.constant.KeysConstant;
import com.songtech.server.constant.TcpServer;
import com.songtech.server.entry.BaseProtocal;
import com.songtech.server.entry.PositionProtocal;
import com.songtech.server.entry.PositionProtocalLBS;
import com.songtech.server.frame.chain.ChainManager;
import com.songtech.server.frame.chain.handler.ElefenceHandler;
import com.songtech.server.frame.chain.handler.EquStatusHandler;
import com.songtech.server.frame.chain.handler.StoragePolicyHandler;
import com.songtech.server.frame.dao.mapper.PositionServerMapper;
import com.songtech.server.frame.route.annotation.Allot;
import com.songtech.server.utils.GetKeysUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.security.Key;
import java.time.LocalTime;

/**
 * Create By YINN on 2018/4/18 16:30
 * Description :
 */

@Allot(cmd = "V3", beanClass = PositionProtocalLBS.class)
@Service
public class LBSPositionServerServiceImpl implements BaseService {

    @Resource
    @Qualifier("jedisBaseDaoImpl")
    protected JedisBaseDao<Serializable> jedisBaseDao;

    @Autowired
    protected PositionServerMapper positionServerMapper;

    @Override
    public void invoke(BaseProtocal baseProtocal) {

        PositionProtocalLBS entry = (PositionProtocalLBS) baseProtocal;

        entry.setAll();

        PositionProtocal castedEntry = entry.castToPositionProtocal();

        if (castedEntry != null) {

            ChainManager chainManager = new ChainManager();

            //处理链
            chainManager
                    .addLast(new ElefenceHandler(castedEntry, jedisBaseDao))
                    .addLast(new EquStatusHandler(castedEntry, jedisBaseDao))
                    .addLast(new StoragePolicyHandler(castedEntry, jedisBaseDao, positionServerMapper))
                    .trigger();

            //存储电量
            jedisBaseDao.put(GetKeysUtils.combineAnyKeys(KeysConstant.DUMP_ENERGY, castedEntry.getImei()), entry.getDumpEnergy());

            //存入缓存
            jedisBaseDao.put(castedEntry.getRedisKey(), castedEntry);

            //存入缓存
            jedisBaseDao.put(GetKeysUtils.combineAnyKeys(KeysConstant.TERMINAL_POSITION_SHOW_KEY, castedEntry.getImei()), castedEntry);
        }

    }
}
